<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/admin/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
    public function logIn(){
        return view('pages.login');
    }
    public function postLogIn(Request $request){
        $userName = $request['username'];
        $password = $request['password'];
        $validateParams = [
            'email' => $userName,
            'password' => $password
        ];
        if (Auth::attempt($validateParams,1)) {
            return $this->returnInfo(true, '登录成功。');
        }else{
            return $this->returnInfo(false, '账号或密码输入错误。');
        }
    }

    private function returnInfo($res, $msg, $url='',$debug=''){
        return response()->json(['res'=>$res, 'info'=>$msg, 'url'=>$url, 'debug'=>$debug]);
    }

    function logout(){
        Auth::logout();
        return response()->json(['res'=>true, 'info'=>'退出成功']);
    }

    public function regist(Request $request){
        $userName = $request['username'];
        $pwd = $request['pwd'];
        $email = $request['email'];
        echo $userName.$pwd.$email;
    }
}
